-- Copyright 2013 ECHO Games
-- This file is a part of Galaga Revolution
-- Galaga Revolution is free software licensed under the GNU General Public License version 3.
-- You may redistribute and/or modify it under the terms of the GNU General Public License
-- version 3 as published by the Free Software Foundation.updater
-- Galaga Revolution comes without a warranty; without even the implied warranty of merchantability
-- or fitness for a particular purpose. See the GNU General Public License version 3 for more
-- details.
-- You should have received the GNU General Public License version 3 with this in GPL.txt
-- if not it is available at <http://www.gnu.org/licenses/gpl.txt>.
-- All resources for Galaga Revolution including: source code, compiled jar, documentation, and source
-- notes are available at: <http://code.google.com/p/echo-gaming-galaga-revolution/>

require "player"
require "map"
require "menu"

function love.load()
	medium = love.graphics.newFont(45)
	gamestate = "menu"
	love.graphics.setBackgroundColor(0,0,0)
	--Buttons!
	button_spawn(240,200,"Start","start")
	button_spawn(245,275,"Quit","quit")
	function love.keyreleased(key)
    if (key == " ") then
        shoot()
    end
end
enemies = {}
for i=0,7 do
enemy = {}
enemy.x = 280
enemy.y = 5
enemy.xvel = 0
enemy.yvel = 0
enemy.speed = .5
enemy.friction = 3.5
enemy.health = 1
enemy.damage = 1
table.insert(enemies, enemy)
end

enemy.pic = love.graphics.newImage("zombie.jpg")

function enemy_draw()
	love.graphics.setColor(255,255,255)
	love.graphics.draw(enemy.pic,enemy.x,enemy.y)
end
function enemy_control(dt)
	enemy.y = enemy.y + enemy.yvel	
	enemy.yvel = enemy.yvel * (1 - math.min(dt*enemy.friction, 1))

	if enemy.yvel > -100 then
		enemy.yvel = enemy.yvel + enemy.speed * dt
	end
end

end
function love.update(dt)
	mousex = love.mouse.getX()
	mousey = love.mouse.getY()
	if gamestate == "playing" then
		player_control(dt)
		enemy_control(dt)
		map_collide()
	end
	if gamestate == "menu" then
		button_check()
	end
    local remEnemy = {}
    local remShot = {}
    
    -- update the shots
    for i,v in ipairs(player.shots) do
    
        -- move them up up up
        v.y = v.y - dt * 100
        
        -- mark shots that are not visible for removal
        if v.y < 0 then
            table.insert(remShot, i)
        end
        
        -- check for collision with enemies
        for ii,vv in ipairs(enemies) do
            if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
                
                -- mark that enemy for removal
                table.insert(remEnemy, ii)
                -- mark the shot to be removed
                table.insert(remShot, i)         
            end
        end    
    end
    -- remove the marked enemies
    for i,v in ipairs(remEnemy) do
        table.remove(enemies, v)
    end
    for i,v in ipairs(remShot) do
        table.remove(player.shots, v)
    end
    -- update those evil enemies
    for i,v in ipairs(enemies) do
        -- let them fall down slowly
        v.y = v.y + dt
        
        -- check for collision with ground
        if v.y > 465 then
            -- you loose!!!
        end    
    end
	if gamestate == "menu" then
		button_check()
	end
end
function love.draw()
	if gamestate == "playing" then
		player_draw()
		enemy_draw()
	end
	if gamestate == "menu" then
		button_draw()
		picture_draw()
	end

    -- let's draw our heros shots
    love.graphics.setColor(255,255,255,255)
    for i,v in ipairs(player.shots) do
        love.graphics.rectangle("fill", v.x, v.y, 2, 5)
    end

end
function shoot()
    
    local shot = {}
    shot.x = player.x+player.width/2
    shot.y = player.y
    
    table.insert(player.shots, shot) 
end

-- Collision detection function.
-- Checks if a and b overlap.
-- w and h mean width and height.
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)

  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end

function love.mousepressed(x,y)
	if gamestate == "menu" then
		button_click(x,y)
	end
end
